$NG.AllReady(function (page, { useUpdateRow, useBeforeClick ,useValuesChange, useUpdateRows }) {
    const mstform = $NG.getCmpApi("p_form_pc_liquid_m");
    const grid = $NG.getCmpApi("p_form_pc_liquid_d");
    const grid2 = $NG.getCmpApi("p_form_pc_liquid_d2");
    const grid3 = $NG.getCmpApi("p_form_pc_liquid_d3");
    const grid4= $NG.getCmpApi("p_form_pc_liquid_d4");
    const grid5= $NG.getCmpApi("p_form_pc_liquid_d5");
    // const grid = $NG.getCmpApi("p_form_pc_liquid_d");
    if (page.oType == "add") {
        filterCntMain()
        getPtInfoByPhid()
        getCntMInfoByPhid()
        getFbsInPay()
        getGysInPayInfo()
        getPdwlTotalInfo()
        getXshtTotalInfo()
        getCntCount("p_form_pc_liquid_m","u_cnt_name",mainCnt)
        var pc = mstform.getItem("phid_pc");
        console.log(pc.getValue());
        console.log("123123");
        if (pc.getValue() > "0") {
            getQSInfo(pc.getValue())

            var pcname = mstform.getItem("phid_pc").getRawValue().label
            $NG.updateState((updater) => {
                //文本
                updater.data.p_form_pc_liquid_m.setProps({
                    bill_name: pcname + "项目清算",
                });
            });
        }
    }
    // 监听项目部分字段
    const optionHead = {
        _sumAmt:()=>{
            this._u_sr_add_dec = this._u_app_amt - this.u_srqrje
            this._u_cnt_wcl = (Number(mstform.getItem("u_ht_amt_ori").getValue()) || 0) && this._u_app_amt / (Number(mstform.getItem("u_ht_amt_ori").getValue()) || 0)
            this._u_mjllzj = this._u_qsmll - this._u_srmll
            
            $NG.updateState((updater)=>{
                updater.data.p_form_pc_liquid_m.setProps({
                    u_srqrje:this._u_srqrje,
                    u_app_amt:this._u_app_amt,
                    u_sr_add_dec:this._u_sr_add_dec,
                    u_cnt_wcl:this._u_cnt_wcl,
                    u_qsmll:this._u_qsmll,
                    u_srmll:this._u_srmll,
                    u_mjllzj:this._u_mjllzj,
                    u_qkyje:this._u_qkyje,
                })
            })
        },
        get u_cz_byje() {
            return this._u_cz_byje;
        },
        set u_cz_byje(value) {
            this._u_cz_byje = Number(value) || 0;
            this._sumAmt()
        },

        get u_srqrje() {
            return this._u_srqrje;
        },
        set u_srqrje(value) {
            this._u_srqrje = Number(value) || 0;
            this._sumAmt()
        },

        get u_app_amt() {
            return this._u_app_amt;
        },
        set u_app_amt(value) {
            this._u_app_amt = Number(value) || 0;
            this._sumAmt()
        },

        get u_sr_add_dec() {
            return this._u_sr_add_dec;
        },
        set u_sr_add_dec(value) {
            this._u_sr_add_dec = Number(value) || 0;
            this._sumAmt()
        },

        get u_cnt_wcl() {
            return this._u_cnt_wcl;
        },
        set u_cnt_wcl(value) {
            this._u_cnt_wcl = Number(value) || 0;
            this._sumAmt()
        },

        get u_qsmll() {
            return this._u_qsmll;
        },
        set u_qsmll(value) {
            this._u_qsmll = Number(value) || 0;
        },

        get u_srmll() {
            return this._u_srmll;
        },
        set u_srmll(value) {
            this._u_srmll = Number(value) || 0;
            this._sumAmt()
        },

        get u_mjllzj() {
            return this._u_mjllzj;
        },
        set u_mjllzj(value) {
            this._u_mjllzj = Number(value) || 0;
            this._sumAmt()
        },

        get u_qkyje() {
            return this._u_qkyje;
        },
        set u_qkyje(value) {
            this._u_qkyje = Number(value) || 0;
            this._sumAmt()
        },

        get u_sywzjr() {
            return this._u_sywzjr;
        },
        set u_sywzjr(value) {
            this._u_sywzjr = Number(value) || 0;
            this._sumAmt()
        }
    };

    // 定义下划线属性并设置为不可枚举
    Object.defineProperties(optionHead, {
    _u_cz_byje: { value: 0, writable: true, enumerable: false, configurable: false },
    _u_srqrje: { value: 0, writable: true, enumerable: false, configurable: false },
    _u_app_amt: { value: 0, writable: true, enumerable: false, configurable: false },
    _u_sr_add_dec: { value: 0, writable: true, enumerable: false, configurable: false },
    _u_cnt_wcl: { value: 0, writable: true, enumerable: false, configurable: false },
    _u_qsmll: { value: 0, writable: true, enumerable: false, configurable: false },
    _u_srmll: { value: 0, writable: true, enumerable: false, configurable: false },
    _u_mjllzj: { value: 0, writable: true, enumerable: false, configurable: false },
    _u_qkyje: { value: 0, writable: true, enumerable: false, configurable: false },
    _u_sywzjr: { value: 0, writable: true, enumerable: false, configurable: false }
});



    useBeforeClick(async function ({ args }) {
        //按钮点击前事件
        $NG.execServer("getRkYhInfo", { pc: mstform.getItem("phid_pc").getValue() }, function (res) {
            console.log(res);
            const data = JSON.parse(res.data);
            const ce = data[0].extendObjects.ce;
            if (ce) {
                $NG.alert("存在要货单的物资未入库，全部入库后方可进行项目清算")
                return false;
            } else {

            }
        });
        // return false; // 取消向下执行
    }, "save");

    //项目名称变化时自动带入产品线、事业部等
    useValuesChange(({ args }) => {

        console.log(args[0]);
        //const phid_pc = args[0].pc.PhId;
        const phid_pc = args[0].phid_pc.value;
        console.log(phid_pc);
        //从项目取值
        if (phid_pc) {
            console.log(phid_pc);
            const mstform = $NG.getCmpApi("p_form_pc_liquid_m");
            getQSInfo(phid_pc)

            var pcname = mstform.getItem("phid_pc").getRawValue().label
            $NG.updateState((updater) => {
                //文本
                updater.data.p_form_pc_liquid_m.setProps({
                    bill_name: pcname + "项目清算",
                });
            });
        }
        getCntCount("p_form_pc_liquid_m","u_cnt_name",mainCnt)
        getPtInfoByPhid()
        getCbjyjlTotalAmt()
        getSsxmJlTotalAmt()
        getCgrkTotalAmt()
        getSybyByPhid()
        filterCntMain()
        getSbqdHeadInfoByPhidPc()
        getGcsjTotalAmt()
        getFbsInPay()
        getGysInPayInfo()
        getFbsInPay()
        getGysInPayInfo()
        getPdwlTotalInfo()
        getXshtTotalInfo()
    }, "p_form_pc_liquid_m.phid_pc");

    function mainCnt(){
        getCntMInfoByPhid()
        
    }

    useValuesChange(({ args }) => {
        mainCnt()
    }, "p_form_pc_liquid_m.u_cnt_name");

    useUpdateRows(({ args }) => {
        sumAmt()
    }, "p_form_pc_liquid_d");

    useUpdateRows(({args})=>{
        const rows = args[0]
        rows.forEach(item=>{
            item.u_kpwsn = (Number(item.u_ykphs) || 0) / 0.09,
            item.u_kpwssix = (Number(item.u_kphssix) || 0) / 0.06,
            item.u_kpwsth = (Number(item.u_kphsth) || 0) / 0.03,
            item.u_kpwsone = (Number(item.u_kphsone) || 0) / 0.01,
            item.u_kpwsthy = (Number(item.u_kphsthy) || 0) / 0.13,
            item.u_kpwsxj = item.u_kpwsn + item.u_kpwssix + item.u_kpwsth + 
                            item.u_kpwsone + item.u_kpwsthy + item.u_kpwsthy
        })
        grid2.refreshView()
    },"p_form_pc_liquid_d2")
    useUpdateRows(({args})=>{
        
    },"p_form_pc_liquid_d3")
    useUpdateRows(({args})=>{
        const rows = args[0]
        rows.forEach(item=>{
            item.u_jypdcy = item.u_jysl - item.u_pdsl
            item.u_dhslcy = item.u_dhsl - item.u_slsl
        })
        grid4.refreshView()
    },"p_form_pc_liquid_d4")
    useUpdateRows(({args})=>{
        const rows = args[0]
        rows.forEach(item=>{
            item.u_wkpsq = item.u_sdje - (Number(item.u_ykpsq) || 0)
            item.u_wkpsl = item.u_sdje - (Number(item.u_ykpsl) || 0)
            item.u_wkpss = item.u_sdje - (Number(item.u_ykpss) || 0)
            item.u_wkpj = item.u_sdje - (Number(item.u_ykpj ) || 0)
            item.u_wkpl = item.u_sdje - (Number(item.u_ykpl) || 0)
            item.u_wkpli =item.u_sdje - (Number(item.u_ykpli) || 0)
            item.u_wkphj  = item.u_wkpsq+ item.u_wkpsl+item.u_wkpss +item.u_wkpj+item.u_wkpl +item.u_wkpli

            item.u_wqrsrsq = item.u_sdje - (Number(item.u_yqrsrsq) || 0)
            item.u_wqrsrsl = item.u_sdje - (Number(item.u_yqrsrsl) || 0)
            item.u_wqrsrss = item.u_sdje - (Number(item.u_yqrsrss) || 0)
            item.u_wqrsrj = item.u_sdje - (Number(item.u_yqrsrj) || 0)
            item.u_wqrsrl = item.u_sdje - (Number(item.u_yqrsrl) || 0)
            item.u_wqrsrli = item.u_sdje - (Number(item.u_yqrsrli) || 0)
            item.u_wqrsrhj = item.u_wqrsrsq + item.u_wqrsrsl + item.u_wqrsrss + item.u_wqrsrj  +item.u_wqrsrl + item.u_wqrsrli
            item.u_srhj = item.u_sdje
        })
        grid5.refreshView()
    },"p_form_pc_liquid_d5")
    function sumAmt(){
        const rows1 = grid.getRows()
        let czTotal = 0
        rows1.forEach((item,index) => {
            // 使用可选链操作符 (?.) 来安全地访问属性，并提供默认值 '0' 以防属性不存在或为非数字
            const amt = parseFloat(item.amt ?? '0');
            const u_sscb = parseFloat(item.u_sscb ?? '0');
            const u_fsscb = parseFloat(item.u_fsscb ?? '0');
            const u_qzbg = parseFloat(item.u_qzbg ?? '0');
            const u_yrzcb = parseFloat(item.u_yrzcb ?? '0');
            const u_hqcb = parseFloat(item.u_hqcb ?? '0');

            // 只有当所有需要的字段都存在并且是有效的数字时才进行计算
            if (!isNaN(amt) && !isNaN(u_sscb) && !isNaN(u_fsscb) && !isNaN(u_qzbg) && !isNaN(u_yrzcb) && !isNaN(u_hqcb)) {
                item.u_sjhj = amt + u_sscb + u_fsscb + u_qzbg;
                item.u_total = u_yrzcb + u_hqcb;
                item.u_jy = item.u_sjhj - item.u_total;
            }
            czTotal += (Number(item.u_total) || 0)
        })

        $NG.updateState((updater)=>{
            updater.data.p_form_pc_liquid_m.setProps({
                u_cz_byje: czTotal
            })
        })
        grid.refreshView()
    }

        //获取合同信息
    function getCntMInfoByPhid(params) {
      if(!mstform.getItem("u_cnt_name").getValue()) return
      $NG.execServer("getCntMInfoByPhid", { phid: mstform.getItem("u_cnt_name").getValue() }, function (res) {
        const datas = JSON.parse(res.data)[0].extendObjects;
        console.log('获取合同信息',datas);
        $NG.updateState((updater) => {
          updater.data.p_form_pc_liquid_m.setProps({
            u_cnt_no: datas.pcm_no, //合同编号
            // start_dt: datas.start_dt, //约定开工时间
            // end_dt: datas.end_dt, //约定完工时间
            u_ht_amt_ori: datas.cnt_sum,
            u_total_cnt_amt_vat: datas.cnt_sum_vat_fc, //合同金额
            u_cnt_amt_vat: datas.cnt_sum_vat_fc, //合同金额
            u_htxe: datas.cnt_sum_vat_fc, //合同价税合计
            //产品线
            user_product_line: {
              value: datas.midwife_phid,
              label: datas.midwife_name,
            },
            //合同类型
            u_cnt_type: {
              value: datas.pct_id,
              label: datas.pct_name,
            },
            //甲方单位
            u_customer: {
              value: datas.customer_id,
              label: datas.customer_name,
            },
            //乙方单位
            supply: {
              value: datas.supply_id,
              label: datas.supply_name,
            },
            //发票类型
            u_invoice_type: {
              value: datas.invoice_type_phid,
              label: datas.invoice_type_name,
            },
            //购方信息（客户信息）（收票单位信息）
            //单位名称
            tick_id: {
              value: datas.tick_id,
              label: datas.tick_name,
            },
            //单位编码
            tick_no: datas.tick_no,
            //社会信用代码
            tick_unisocialcredit: datas.tick_unisocialcredit,
            //单位地址
            tick_taxaddress: datas.tick_taxaddress,
            //联系电话
            tick_taxtelephone: datas.tick_taxtelephone,
            //银行名称
            tick_taxbankid: {
              value: datas.tick_taxbankid,
              label: datas.tick_taxbankname,
            },
            //银行账号
            tick_taxaccountno: datas.tick_taxaccountno,
            //销方信息（供应商信息）（开票单位信息）
            //单位名称
            inv_id: { value: datas.inv_id, label: datas.inv_name },
            //单位编码
            inv_no: datas.inv_no,
            //社会信用代码
            inv_unisocialcredit: datas.inv_unisocialcredit,
            //单位地址
            inv_taxaddress: datas.inv_taxaddress,
            //联系电话
            inv_taxtelephone: datas.inv_taxtelephone,
            //银行名称
            inv_taxbankid: {
              value: datas.inv_taxbankid,
              label: datas.inv_taxbankname,
            },
            //银行账号
            inv_taxaccountno: datas.inv_taxaccountno,
          });
        });
        // getQzInfo();
      });
    }
//根据项目筛选合同
    function filterCntMain() {
      $NG.updateUI((updater, state) => {
        console.log("过滤参数检查", updater);
        updater.fieldSetForm.p_form_pc_liquid_m.u_cnt_name.setProps({
          onBeforeOpen: async () => {
            var phid_pc = mstform.getItem("phid_pc").getValue();
            if (!phid_pc) {
              $NG.alert("请先选择项目");
              return false;
            } else {
              return true;
            }
          },
          ...(!!mstform.getItem("phid_pc").getValue() && {
            clientSqlFilter: {
              bill_type: 1, //合同类型
              phid_pc: mstform.getItem("phid_pc").getValue(),
            },
          }),
        });
      });
    }
    function getPtInfoByPhid() {
        if (mstform.getItem("phid_pc").getValue()) {
        $NG.execServer("getPtInfoByPhid", { phid: mstform.getItem("phid_pc").getValue() }, function (res) {
            const datas = JSON.parse(res.data)[0].extendObjects;
            console.log(`项目信息`,datas);
            //帮助字段赋值
            $NG.updateState((updater) => {
            updater.data.p_form_pc_liquid_m.setProps({
                u_pc_no: datas.pc_no, //项目编号
                u_agreed_sts_dt: datas.start_date, //计划完工日期
                u_agreed_pcl_dt: datas.end_date, //计划竣工日期
                u_xmjl: {
					value: datas.project_manager_phid,
					label: datas.project_manager_name,
				},
                u_sqgw: {
                //售前顾问
                    value: datas.user_sqgw_phid,
                    label: datas.user_sqgw_name,
                },
                //事业部
                u_division: {
                value: datas.division_phid,
                label: datas.division_name,
                },
                //大区
                u_region: {
                value: datas.phid_org_phid,
                label: datas.phid_org_name,
                },
                u_regional_center: {
                //区域中心
                value: datas.regional_center_phid,
                label: datas.regional_center_name,
                },
                u_product_line: {
                //产品线
                value: datas.midwife_phid,
                label: datas.midwife_name,
                },
                phid_org: {
                //部门
                value: datas.dept_phid,
                label: datas.dept_name,
                },
                u_pc_type: {
                //项目类型
                value: datas.type_phid,
                label: datas.type_name,
                },
            });
            });
        });
        }
    }
    // 获取采购入库的相关金额
    page.oType != 'view' && getCgrkTotalAmt()
        function getCgrkTotalAmt(){
            const phid_pc = mstform.getItem("phid_pc").getValue()
            if(!phid_pc) return 
            $NG.execServer("getCgrkTotalAmt",{phid_pc},(res)=>{
                if(res.count == 0) return
                const _data = JSON.parse(res.data)[0].extendObjects
                $NG.updateState((updater)=>{
                    updater.data.p_form_pc_liquid_m.setProps({
                        u_cgcb:(Number(_data.total_amt) || 0)
                    })
                })
            })
        }
    page.oType != 'view' && getSsxmJlTotalAmt()
    // 获取实施项目激励的费用合计
    function getSsxmJlTotalAmt(){
        const phid_pc = mstform.getItem("phid_pc").getValue()
        if(!phid_pc) return 
        $NG.execServer("getSsxmJlTotalAmt",{phid_pc},(res)=>{
            if(res.count == 0) return 
            const _data = JSON.parse(res.data)[0].extendObjects
            $NG.updateState((updater)=>{
                updater.data.p_form_pc_liquid_m.setProps({
                    u_ssjl:(Number(_data.total_amt) || 0)
                })
            })    
        })
    }

    page.oType != 'view' && getCbjyjlTotalAmt()
    // 获取成本结余激励费用
    function getCbjyjlTotalAmt(){
        const phid_pc = mstform.getItem("phid_pc").getValue()
        if(!phid_pc) return 
        $NG.execServer("getCbjyjlTotalAmt",{phid_pc},(res)=>{
            if(res.count == 0) return
            const _data = JSON.parse(res.data)[0].extendObjects
            $NG.updateState((updater)=>{
                updater.data.p_form_pc_liquid_m.setProps({
                    u_cbjyjl:(Number(_data.total_amt) || 0)
                })
            })
        })
        
    } 
    page.oType != 'view' && getSybyByPhid()
    // 获取收入报验金额
    function getSybyByPhid(){
        const phid_pc = mstform.getItem("phid_pc").getValue()
        if(!phid_pc) return
        $NG.execServer("getSybyByPhid",{phid_pc},(res)=>{
            if(res.count == 0) return 
            const _data = JSON.parse(res.data)[0].extendObjects
            optionHead.u_cz_byje = (Number(_data.total_amt_vat) || 0)
            // $NG.updateState((updater)=>{
            //     updater.data.p_form_pc_liquid_m.setProps({
            //         u_cz_byje:(Number(_data.total_amt_vat) || 0)
            //     })
            // })
        })
    }

    page.oType != 'view' && getSbqdHeadInfoByPhidPc()
    // 获取设备清单表头信息
    function getSbqdHeadInfoByPhidPc(){
        const phid_pc = mstform.getItem("phid_pc").getValue()
        if(!phid_pc) return 
        $NG.execServer("getSbqdHeadInfoByPhidPc",{phid_pc},(res)=>{
            if(res.count == 0) return
            const _data = JSON.parse(res.data)[0].extendObjects

            $NG.updateState((updater)=>{
                updater.data.p_form_pc_liquid_m.setProps({
                    u_qdmll:(Number(_data.u_signed_gm_rate) || 0),
                    u_dqmll:(Number(_data.u_bid_gm_rate) || 0)
                })
            })
        })
    }

    page.oType != 'view' && getGcsjTotalAmt()
    // 获取工程审计金额
    function getGcsjTotalAmt(){
        const phid_pc = mstform.getItem("phid_pc").getValue()
        if(!phid_pc) return 
        $NG.execServer("getGcsjTotalAmt",{phid_pc},(res)=>{
            if(res.count == 0) return 
            const _data = JSON.parse(res.data)[0].extendObjects
            optionHead.u_app_amt = (Number(_data.total_amt) || 0)
            // $NG.updateState((updater)=>{
            //     updater.data.p_form_pc_liquid_m.setProps({
            //         u_app_amt:(Number(_data.total_amt) || 0)
            //     })
            // })
        })
    }
    // 根据项目获取分包商付款信息
    
    function getFbsInPay(){
        const phid_pc = mstform.getItem("phid_pc").getValue()
        if(!phid_pc) return
        $NG.execServer("getFbsInPay",{phid_pc},(res)=>{
            const _data = JSON.parse(res.data).reduce((value,item)=>{
                const el = item.extendObjects
                const va = value[el.cntphid]
                if(va){
                   value[el.cntphid][el.fp_rate] = el
                } else {
                     value[el.cntphid] = {}
                    value[el.cntphid][el.fp_rate] = el
                    
                }
                return value
            },{})
            window._catch_test1 = _data
            const rows = []
            for(let key in _data){
                const _row = {
                    u_fbxmh:_data[key].cnt_no,
                    u_fbdw:_data[key].fbdw_phid,
                    u_fbdw_EXName:_data[key].fbdw_name,
                    u_fbhshte:_data[key].fp_amt_vat,
                    u_fbhtsl:_data[key].cnt_taxrate
                }
                for(let k in _data[key]){
                    const element = _data[key][k]
                    let _key = ''
                    switch((Number(k) || 0)){
                        case 0.06:
                            _key = 'u_kphssix'
                            break
                        case 0.09:
                            _key = 'u_ykphs'
                            break
                        case 0.03:
                            _key = 'u_kphsth'
                            break
                        case 0.01:
                            _key = 'u_kphsone'
                            break
                        case 0.13:
                            _key = 'u_kphsthy' 
                            break
                    }
                    if(_row[_key]){
                        _row[_key] = (Number(element.fp_amt_vat) || 0)
                    } else {
                        _row[_key] += (Number(element.fp_amt_vat) || 0)
                    }
                }
                //小计
                const amt_key = ["u_kphssix","u_ykphs","u_kphsth","u_kphsone","u_kphsthy"]
                _row.u_xj = amt_key.reduce((value,item)=>{
                    value += (Number(_row[item]) || 0)
                    return value
                },0)
                rows.push({..._row})
            }
            console.log(`分包商明细`,rows);
            grid2.clearRows().then(()=>{
                grid2.addRows([...rows])
            })
        })
    }
    // 根据项目获取采购合同机器采购应付单的相关信息
    
    function getGysInPayInfo(){
        const phid_pc = mstform.getItem("phid_pc").getValue()
        if(!phid_pc) return 
        $NG.execServer("getGysInPayInfo",{phid_pc},(res)=>{
            if(res.count == 0) return
            const _data = JSON.parse(res.data).reduce((value,item)=>{
                const el = item.extendObjects
                value.push({
                    u_cgdhh:el.cnt_no,
                    u_gys_EXName:el.jfdw_name,
                    u_gys:el.jfdw_phid,
                    u_cgddqd:el.cnt_amt_vat,
                    u_ykpje:el.bckp_amt,
                    u_yfkje:el.bqyf_amt_vat,
                    u_wkpje:(Number(el.dz_yf_amt_vat) || 0) - (Number(el.bckp_amt) || 0)
                })
                return value
            },[])
            grid3.clearRows().then(()=>{
                grid3.addRows([..._data])
            })
        })
    }
    // 根据项目获取采购入库分包领料盘点物料信息
    function getPdwlTotalInfo(){
        const phid_pc = mstform.getItem("phid_pc").getValue()
        if(!phid_pc) return 
        $NG.execServer("getPdwlTotalInfo",{phid_pc},(res)=>{
            if(res.count == 0) return 
            const _data = JSON.parse(res.data).reduce((value,item)=>{
                const el = item.extendObjects
                value.push({
                    u_wlbm:el.itemno,
                    u_wlmc:el.phid_itemdata,
                    u_wlmc_EXName:el.phid_itemname,
                    u_dhsl:el.dh_qty,
                    u_slsl:el.sl_qty,
                    u_cksl:el.ll_qty,
                    u_jysl:el.sy_qty,
                    u_pdsl:el.pd_qty,
                })
                return value
            },[])
            grid4.clearRows().then(()=>{
                grid4.addRows([..._data])
            })
        })
    }
    // 根据项目获取销售合同的相关信息汇总
    function getXshtTotalInfo(){
        const phid_pc = mstform.getItem("phid_pc").getValue()
        if(!phid_pc) return 
        $NG.execServer("getXshtTotalInfo",{phid_pc},(res)=>{
            if(res.count == 0) return 
            const _data = JSON.parse(res.data).reduce((value,item)=>{
                const el = item.extendObjects
                const temp = value[el.cntphid]
                if(temp){
                    if(temp[el.taxrate]){
                        temp[el.taxrate] += (Number(el.kp_amt_vat) || 0)
                    }
                } else {
                    value[el.cntphid]={}
                    value[el.cntphid]['item'] = el
                    value[el.cntphid][el.taxrate] = (Number(el.kp_amt_vat) || 0)
                }
                return value
            },{})
            $NG.execServer("getSrbyTotalInfo",{phid_pc},(res)=>{
                let datas = []
                if(res.count != 0){
                    datas = JSON.parse(res.data).reduce((value,item)=>{
                        value.push(item.extendObjects)
                        return value
                    },[])
                }

                window._catch_sybyData = _data
                const rows = []
                for(let key in window._catch_sybyData){
                    const item = window._catch_sybyData[key]
                    const el = item.item
                    let by = datas.filter(it=>{
                        if(it.u_cnt_name == el.cntphid){
                            return it
                        }
                    })[0]
                    if(!by){
                        by = {}
                    }
                    rows.push({
                        u_kh:el.kh_phid,
                        u_kh_EXName:el.kh_name,
                        u_cshte:el.init_cnt_amt_vat,
                        u_htxe:el.cnt_amt_vat,
                        u_yhkhj:el.cnt_in_pay_amt,
                        u_sdje:el.u_jstzje,
                        u_ykpsq:el['0.17'],
                        u_ykpsl:el['0.16'],
                        u_ykpss:el['0.13'],
                        u_ykpj:el['0.09'],
                        u_ykpl:el['0.06'],
                        u_ykpli:el['0'],
                        u_ykphj:(Number(el['0.17']) || 0) 
                        + (Number(el['0.16'] ) || 0)
                        + (Number(el['0.13'] ) || 0)
                        + (Number(el['0.09'] ) || 0)
                        + (Number(el['0.06'] ) || 0)
                        + (Number(el['0']) || 0),
                        u_yqrsrsq:by['0.17'],
                        u_yqrsrsl:by['0.16'],
                        u_yqrsrss:by['0.13'],
                        u_yqrsrj:by['0.09'],
                        u_yqrsrl:by['0.06'],
                        u_yqrsrli:by['0']
                    })
                }
                grid5.clearRows().then(()=>{
                    grid5.addRows([...rows])
                })
            })
            
            
        })
    }
});




function getQSInfo(pc) {
    const grid = $NG.getCmpApi("p_form_pc_liquid_d");
    grid.clearRows();
    $NG.execServer('GetJSInfo', { pc: pc }, function (res) {
        const data = JSON.parse(res.data);
        var gridArr = []; // 定义表体数组
        var gridCount = res.count; // 增加的行数, 可根据从数据库中取出的数据总数增行
        for (var i = 0; i < gridCount; i++) {
            // 列出需要赋值的表体字段
            var gridObj = {
                u_pc_no: '',
                amt: '',
                u_sscb: '',
                u_fsscb: '',
                u_qzbg: '',
                u_yrzcb: '',
            };
            gridObj.u_pc_no = data[i].extendObjects.bh;
            gridObj.amt = data[i].extendObjects.cspsje;
            gridObj.u_sscb = data[i].extendObjects.ssyy;
            gridObj.u_fsscb = data[i].extendObjects.fssyy;
            gridObj.u_qzbg = data[i].extendObjects.bgje;
            gridObj.u_yrzcb = data[i].extendObjects.rzcb;

            gridArr.push(gridObj);
        }

        // 清空表格内容（放在循环外部）
        grid.clearRows();

        // 调用新增表体行数据的函数
        grid.addRows(gridArr);

        // 更新RichHelp字段（如果需要）
        // 这里假设有一个方法可以刷新或重新渲染表格，以确保RichHelp字段正确显示
        grid.refreshView(); // 或者使用grid.refreshData();
    });



}

function getCntCount(table, key, fun) {
      if (!$NG.getCmpApi(table).getItem("phid_pc").getValue()) return;
      $NG.execServer("getCntCount", { type: 1, phid_pc: $NG.getCmpApi(table).getItem("phid_pc").getValue() }, (res) => {
        console.log(res, "count");
        switch (res.count) {
          case 0:
            return;
          case 1:
            const datas = JSON.parse(res.data)[0].extendObjects;
            const temp_map = {};
            temp_map[key] = datas.phid;
            temp_map[`${key}_EXName`] = datas.bill_name;
            $NG.updateState((updater) => {
              updater.data[table].setProps({
                ...temp_map,
              });
              setTimeout(() => {
                if (fun) {
                  fun();
                }
              }, 200);
            });
        }
      });
    }